﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DinhMuc.BUL;
using DinhMuc.Public;

namespace DinhMuc.UI.Frm_Form
{
    public partial class TKSX_TPADD : Form
    {
        public TKSX_TPADD()
        {
            InitializeComponent();
        }
        DataTable dtPublic = new DataTable();
        int indexRow = 0;
        private void TKSX_TPADD_Load(object sender, EventArgs e)
        {
            indexRow = 0;
            dsDMTP();
            dtPublic.Columns.Add("ID_SX");
            dtPublic.Columns.Add("ID_DM");
            dtPublic.Columns.Add("soDH");
            dtPublic.Columns.Add("maNVL");
            dtPublic.Columns.Add("DVT");
            dtPublic.Columns.Add("ngay");
            dtPublic.Columns.Add("ca");
            dtPublic.Columns.Add("phuTrach");
            dtPublic.Columns.Add("dmThoi");
            dtPublic.Columns.Add("thoiTT");
            dtPublic.Columns.Add("dmTrangL1");
            dtPublic.Columns.Add("trangL1TT");
            dtPublic.Columns.Add("dmTrangL2");
            dtPublic.Columns.Add("trangL2TT");
            dtPublic.Columns.Add("dmIn");
            dtPublic.Columns.Add("inTT");
            dtPublic.Columns.Add("dmChiaCuon");
            dtPublic.Columns.Add("chiaCuonTT");
            dtPublic.Columns.Add("dmCatDan");
            dtPublic.Columns.Add("catDanTT");

        }
        private void dsDMTP()
        {
            tblDinhMucBUL dm = new tblDinhMucBUL();
            gridEX1.DropDowns["dsDinhMucTP"].DataSource = dm.dsDinhMucTP1();
        }

        private void gridEX1_CellValueChanged(object sender, Janus.Windows.GridEX.ColumnActionEventArgs e)
        {
            try
            {
                if (e.Column.Key == "soDH")
                {
                    tblDinhMucBUL dm = new tblDinhMucBUL();
                    DataTable dt = dm.bangDMTP(int.Parse(gridEX1.GetValue("soDH").ToString()));
                    if (dt.Rows.Count > 0)
                    {
                        DataRow row = dt.Rows[0];
                        gridEX1.SetValue("ID_DM", row["ID"].ToString());
                        //gridEX1.DropDowns["cot2"] = "";
                        gridEX1.SetValue("soDH", row["soDH"].ToString());
                        gridEX1.SetValue("maNVL", row["maNVL"].ToString());
                        gridEX1.SetValue("DVT", row["DVT"].ToString());
                        gridEX1.SetValue("dmThoi", float.Parse("0"+row["dmThoi"].ToString()));
                        gridEX1.SetValue("dmTrangL1", float.Parse("0"+row["dmTrangL1"].ToString()));
                        gridEX1.SetValue("dmTrangL2", float.Parse("0"+row["dmTrangL2"].ToString()));
                        gridEX1.SetValue("dmIn", float.Parse("0"+row["dmIn"].ToString()));
                        gridEX1.SetValue("dmChiaCuon", float.Parse("0"+row["dmChiaCuon"].ToString()));
                        gridEX1.SetValue("dmCatDan", float.Parse("0"+row["dmCatDan"].ToString()));
                        gridEX1.SetValue("ngay", DateTime.Now.ToString());

                    }

                }
                //if(e.Column.Key=="cot2")
                //{
                //    //tinh toan

                //}
            }
            catch (Exception)
            {
            }
        }

        private void gridEX1_RecordAdded(object sender, EventArgs e)
        {
            indexRow++;
            DataRow row = dtPublic.NewRow();

            row["ID_SX"] = indexRow;
            row["ID_DM"] = gridEX1.GetValue("ID_DM").ToString();
            row["soDH"] = gridEX1.GetValue("soDH").ToString();
            row["maNVL"] = gridEX1.GetValue("maNVL").ToString();
            row["DVT"] = gridEX1.GetValue("DVT").ToString();
            row["ngay"] = gridEX1.GetValue("ngay").ToString();
            row["ca"] = gridEX1.GetValue("ca").ToString();
            row["phuTrach"] = gridEX1.GetValue("phuTrach").ToString();
            row["dmThoi"] = gridEX1.GetValue("dmThoi").ToString();
            row["thoiTT"] = gridEX1.GetValue("thoiTT").ToString();
            row["dmTrangL1"] = gridEX1.GetValue("dmTrangL1").ToString();
            row["trangL1TT"] = gridEX1.GetValue("trangL1TT").ToString();
            row["trangL2TT"] = gridEX1.GetValue("trangL2TT").ToString();
            row["dmIn"] = gridEX1.GetValue("dmIn").ToString();
            row["inTT"] = gridEX1.GetValue("inTT").ToString();
            row["dmChiaCuon"] = gridEX1.GetValue("dmChiaCuon").ToString();
            row["chiaCuonTT"] = gridEX1.GetValue("chiaCuonTT").ToString();
            row["dmCatDan"] = gridEX1.GetValue("dmCatDan").ToString();
            row["catDanTT"] = gridEX1.GetValue("catDanTT").ToString();
            dtPublic.Rows.Add(row); 
           
            //try
            //{
            //    tblTKSX_TPBUL csl = new tblTKSX_TPBUL();
            //    tblTKSX_TPPublic p = new tblTKSX_TPPublic();
            //    p.Id_DM = int.Parse(gridEX1.GetValue("ID_DM").ToString());
            //    p.ThoiTT = float.Parse("0" + gridEX1.GetValue("thoiTT").ToString());
            //    p.TrangL1TT = float.Parse("0" + gridEX1.GetValue("trangL1TT").ToString());
            //    p.TrangL2TT = float.Parse("0" + gridEX1.GetValue("trangL2TT").ToString());
            //    p.InTT = float.Parse("0" + gridEX1.GetValue("inTT").ToString());
            //    p.ChiaCuonTT = float.Parse("0" + gridEX1.GetValue("chiaCuonTT").ToString());
            //    p.CatDanTT = float.Parse("0" + gridEX1.GetValue("catDanTT").ToString());
            //    p.Ngay = Convert.ToDateTime(gridEX1.GetValue("ngay").ToString());
            //    p.Ca = gridEX1.GetValue("ca").ToString();
            //    p.PhuTrach = gridEX1.GetValue("phuTrach").ToString();

            //    csl.themTKSX(p);
            //    gridEX1.MoveToNewRecord();
            //    TKSX_TPADD_Load(sender, e);
            //}
            //catch (Exception)
            //{
            //}
        }
        private void Insert()
        {
            for (int i = 0; i < dtPublic.Rows.Count; i++)
            {
                DataRow row = dtPublic.Rows[i];
                tblTKSX_TPBUL tp = new tblTKSX_TPBUL();
                tblTKSX_TPPublic p = new tblTKSX_TPPublic();
                p.Id_DM = int.Parse(row["ID_DM"].ToString());                           
                p.ThoiTT = float.Parse("0" + row["thoiTT"].ToString());                
                p.TrangL1TT = float.Parse("0" + row["trangL1TT"].ToString());             
                p.TrangL2TT = float.Parse("0" + row["trangL2TT"].ToString());                
                p.InTT = float.Parse("0" + row["inTT"].ToString());                
                p.ChiaCuonTT = float.Parse("0" + row["chiaCuonTT"].ToString());                
                p.CatDanTT = float.Parse("0" + row["catDanTT"].ToString());
                p.Ngay = Convert.ToDateTime(row["ngay"].ToString());
                p.Ca = row["ca"].ToString();
                p.PhuTrach = row["phuTrach"].ToString();

                tp.themTKSX(p);
                gridEX1.MoveToNewRecord();
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            Insert();
            MessageBox.Show("Thêm thành công");
            this.Close();
        }

        private void gridEX1_RecordUpdated(object sender, EventArgs e)
        {
            DataRow row = dtPublic.Rows[gridEX1.CurrentRow.RowIndex];
            row["ID_DM"] = gridEX1.GetValue("ID_DM").ToString();
            row["soDH"] = gridEX1.GetValue("soDH").ToString();
            row["maNVL"] = gridEX1.GetValue("maNVL").ToString();
            row["DVT"] = gridEX1.GetValue("DVT").ToString();
            row["ngay"] = gridEX1.GetValue("ngay").ToString();
            row["ca"] = gridEX1.GetValue("ca").ToString();
            row["phuTrach"] = gridEX1.GetValue("phuTrach").ToString();
            row["dmThoi"] = gridEX1.GetValue("dmThoi").ToString();
            row["thoiTT"] = gridEX1.GetValue("thoiTT").ToString();
            row["dmTrangL1"] = gridEX1.GetValue("dmTrangL1").ToString();
            row["trangL1TT"] = gridEX1.GetValue("trangL1TT").ToString();
            row["trangL2TT"] = gridEX1.GetValue("trangL2TT").ToString();
            row["dmIn"] = gridEX1.GetValue("dmIn").ToString();
            row["inTT"] = gridEX1.GetValue("inTT").ToString();
            row["dmChiaCuon"] = gridEX1.GetValue("dmChiaCuon").ToString();
            row["chiaCuonTT"] = gridEX1.GetValue("chiaCuonTT").ToString();
            row["dmCatDan"] = gridEX1.GetValue("dmCatDan").ToString();
            row["catDanTT"] = gridEX1.GetValue("catDanTT").ToString();

            dtPublic.AcceptChanges();
            gridEX1.DataSource = dtPublic;
        }
    }
}
